#include<bits/stdc++.h>
#define eb emplace_back
#define ep emplace

using namespace std;
using ll = long long;
constexpr int N = 1e5 + 5;

int n, m, b[N], X[N], a[N], c[N], K, d[N];
int g[20][2 * N];

ll solve(int n) {
    int k = __lg(n);
    if((1 << k) != n) ++ k;
    int tot = 1 << k;

    for(int i = 1; i <= n; ++ i) d[i] = a[i];
    set<int> se;

    auto dfs = [&](auto dfs, int dep) {
        if(dep == tot + 1) {
            vector<int> vec;
            for(int i = 1; i <= tot; ++ i) vec.eb(i);
            for(int r = 1; r <= k; ++ r) {
                vector<int> tmp;
                for(int i = 0; i < (1 << k - r); ++ i) {
                    int x = vec[2 * i], y = vec[2 * i + 1];
                    if(g[r][i] && x < y) if(x < y) swap(x, y);
                    if(!g[r][i] && x > y) if(x < y) swap(x, y);

                    if(d[x] >= r) tmp.eb(x);
                    else tmp.eb(y);
                }
                vec = tmp;
            }
            se.ep(vec[0]);
            return;
        }
        for(int i = 0; i <= k; ++ i) {
            d[dep] = i;
            dfs(dfs, dep + 1);
        }
    };
    dfs(dfs, n + 1);

    ll ret = 0;
    for(int x : se) ret += x;
    return ret;
}
int main() {
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    for(int i = 1; i <= n; ++ i) cin >> b[i];
    for(int i = 1; i <= m; ++ i) cin >> c[i];

    int K = __lg(n);
    if((1 << K) != n) ++ K;
    for(int i = 1; i <= K; ++ i) {
        string s; cin >> s;
        for(int j = 0; j < (1 << K - i); ++ j) {
            g[i][j] = s[j] - '0';
        }
    }
    int T; cin >> T;
    while(T --) {
        for(int i = 0; i < 4; ++ i) cin >> X[i];
        for(int i = 1; i <= n; ++ i) {
            a[i] = b[i] ^ X[i % 4];
        }
        ll ans = 0;
        for(int i = 1; i <= m; ++ i) {
            ans ^= (i * solve(c[i]));
        }
        cout << ans << '\n';
    }
    return 0;
}